//
//  OrderCenterViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/11.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "OrderCenterViewController.h"
#import "SMPagerTabView.h"
#import "WaitPayViewController.h"
#import "WaitSendViewController.h"
#import "WaitReceiveViewController.h"
#import "HadReceiveViewController.h"
#import "LogisticsViewController.h"
#import "OrdertoSureVC.h"
#import "OrderCenterList.h"
#import "OrderCenterModel.h"
#import "OrdertoSureVC.h"
#import "PosApplyViewController.h"
#import "NewOrderDetailViewController.h"
@interface OrderCenterViewController ()<SMPagerTabViewDelegate,WaitSendViewControllerDelegate,WaitPayViewControllerDelegate,HadReceiveViewControllerDelegate,WaitReceiveViewControllerrDelegate>

@property (nonatomic, strong) NSMutableArray *vcArr;
@property (nonatomic, strong) SMPagerTabView *segmentView;

@end

@implementation OrderCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"订单中心"];
    
    [self setUI];
}

- (void)setUI{
    
    _vcArr = [NSMutableArray array];
    
    WaitPayViewController *vc1 = [[WaitPayViewController alloc]initWithNibName:nil bundle:nil];
    vc1.delegate = self;
    vc1.title = @"未支付";
    
    WaitSendViewController *vc2 = [[WaitSendViewController alloc]initWithNibName:nil bundle:nil];
    vc2.delegate = self;
    vc2.title = @"待发货";
    
    WaitReceiveViewController *vc3 = [[WaitReceiveViewController alloc]initWithNibName:nil bundle:nil];
    vc3.delegate = self;
    vc3.title = @"待收货";
    
    HadReceiveViewController *vc4 = [[HadReceiveViewController alloc]initWithNibName:nil bundle:nil];
    vc4.delegate = self;
    vc4.title = @"已收货";
    
    [_vcArr addObject:vc1];
    [_vcArr addObject:vc2];
    [_vcArr addObject:vc3];
    [_vcArr addObject:vc4];
    
    
    self.segmentView.delegate = self;
    
    //可自定义背景色和tab button的文字颜色等
    //开始构建UI
    [_segmentView buildUI];
    
    //起始选择一个tab
    [_segmentView selectTabWithIndex:self.index animate:NO];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setIndex) name:@"PaySuccess" object:nil];
}

- (void)setIndex{
    
    [_segmentView selectTabWithIndex:1 animate:NO];
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - DBPagerTabView Delegate
- (NSUInteger)numberOfPagers:(SMPagerTabView *)view {
    return [_vcArr count];
}
- (UIViewController *)pagerViewOfPagers:(SMPagerTabView *)view indexOfPagers:(NSUInteger)number {
    return _vcArr[number];
}

- (void)whenSelectOnPager:(NSUInteger)number {
    NSLog(@"页面 %lu",(unsigned long)number);
}

#pragma mark - setter/getter
- (SMPagerTabView *)segmentView {
    if (!_segmentView) {
        self.segmentView = [[SMPagerTabView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - (iPhoneX_Top))];
        [self.view addSubview:_segmentView];
    }
    return _segmentView;
}


#pragma mark - 点击cell
- (void)onCurrentCell{
    
    NewOrderDetailViewController *VC = [[NewOrderDetailViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 查看物流
- (void)lookLogistics{
    
    if ([OrderCenterList sharedInstance].selectedModel.logisticsName.length <= 0) {
        
        return [self showMBPError:@"未查询到物流公司"];
    }
    
    if ([OrderCenterList sharedInstance].selectedModel.expressNumber.length <= 0) {
        
        return [self showMBPError:@"未查询到物流编号"];
    }
    
    LogisticsViewController *VC = [[LogisticsViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 付款
- (void)onPay{
    
    OrderCenterModel *model = [OrderCenterList sharedInstance].selectedModel;
    
    OrdertoSureVC *VC = [[OrdertoSureVC alloc]init];
    VC.tag = @"订单中心";
    VC.price = model.totalAmount;
    VC.OrderID = model.uid;
    [self.navigationController pushViewController:VC animated:YES];
    
}



- (void)back{
    
    if ([self.tag isEqualToString:@"机具申领"]) {
        
        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:[PosApplyViewController class]]) {
                
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }else if([self.tag isEqualToString:@"订单中心"]){
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}






@end
